package exercise;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Map的遍历
 *
 * @author luoyalan
 * @date 2018/11/25
 */
public class MapDemo {
    public static void main(String[] args) {
        Map<String, String> stringMap = new HashMap<String, String>();
        for (int i = 0; i < 10; i++) {
            stringMap.put(i + "", i + "");
        }
        //  使用entrySet遍历Map集合
        Long time1 = System.currentTimeMillis();
        Iterator iterator = stringMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            System.out.println(key + " : " + value);
        }
        System.out.println(System.currentTimeMillis() - time1);

        // 使用keySet遍历集合
        Long time2 = System.currentTimeMillis();
        Iterator iterator1 = stringMap.keySet().iterator();
        while (iterator1.hasNext()) {
            String key = (String) iterator1.next();
            String value = stringMap.get(key);
            System.out.println(key + " : " + value);
        }
        System.out.println(System.currentTimeMillis() - time2);
    }
}
